package com.SeleniumTestsGoogleChrome;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.regex.Pattern;

import com.thoughtworks.selenium.SeleneseTestBase;

public class FlightStatisticsTestCases extends SeleneseTestBase {

	@Before
	public void setUp() throws Exception {
		File file = new File(
				"C:/Users/Steven/Documents/4th Year/EECE 419/chromedriver.exe");
		System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
		WebDriver driver = new ChromeDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}
	
	// Created by Steven Chow
		// Scenario 1: Flight Manifest Displayed
		// Test Case 1: Everything is displayed
		@Test
		public void test1() throws Exception {
			// Go to website
			selenium.open("");
			// Login as an Admin
			selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=id", "1");
			selenium.type("name=password", "pass0");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Manager Portal
			assertEquals("Manager Portal", selenium.getTitle());
			// Check if the "Flight Statistics" Button is present
			assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Stats Page
			assertEquals("Statistics", selenium.getTitle());
			// Check for a Flight with the reservation is there
			assertEquals(
					"1002 2012-12-12 14:00 Air Canada Williams Lake BlueBearLand 200.0 122",
					selenium.getText("//html/body/div/div[2]/div/div/div/div/form/table/tbody/tr/"));
			selenium.click("name=r1");
			selenium.click("name=view");
			selenium.waitForPageToLoad("30000");
			// Look for the passenger tab
			selenium.click("link=Passengers");
			assertEquals("Passengers",
					selenium.getText("//html/body/div/div[2]/div/div/ul/li/a"));
			// Check if all info is there
			assertEquals(
					"Very Cute Girl F19 Economy Mcdonalds",
					selenium.getText("//html/body/div/div[2]/div/div/div/div/table/tbody/tr"));
			// Check if the Summary tab is there
			assertEquals("Summary",
					selenium.getText("//html/body/div/div[2]/div/div/ul/li[2]/a"));
			selenium.click("link=Summary");
			// Check if everything is there
			assertEquals(
					"Air Canada Williams Lake 2012-12-12 14:00 BlueBearLand 2012-12-12 23:00",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[2]/table/tbody"));
			assertEquals(
					"0 0 1 1 $100.0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[2]/table[2]/tbody"));
			assertEquals(
					"0 0 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[2]/table[3]/tbody/tr"));
		}

		// Created by Steven Chow
		// Scenario 2: Flight Manifest Not Displayed
		// Test Case 1: No data present
		@Test
		public void test2() throws Exception {

			// Go to website
			selenium.open("");
			// Login as an Admin
			selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=id", "1");
			selenium.type("name=password", "pass0");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Manager Portal
			assertEquals("Manager Portal", selenium.getTitle());
			// Check if the "Flight Statistics" Button is present
			assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Stats Page
			assertEquals("Statistics", selenium.getTitle());
			// Check if we get a Message
			assertTrue(selenium
					.isTextPresent("No Data To Display. No Reservations are made."));

		}

		// Created by Steven Chow
		// Scenario 3 Flight Stats are displayed
		// Test Case: All 4 of them
		@Test
		public void test3() throws Exception {
			// Go to website
			selenium.open("");
			// Login as an Admin
			selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=id", "1");
			selenium.type("name=password", "pass0");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Manager Portal
			assertEquals("Manager Portal", selenium.getTitle());
			// Check if the "Flight Statistics" Button is present
			assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Stats Page
			assertEquals("Statistics", selenium.getTitle());
			// Check out the Seat Class Stats
			selenium.click("link=Seat Class Stats");
			assertEquals(
					"Seat Class Count across all flights Business 0 Economy 1 Economy Plus 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[2]/table"));
			// Check out the Meal Stats
			selenium.click("link=Meal Stats");
			assertEquals(
					"Meal Count across all flights Mcdonalds 1 Beef 0 Chicken 0 Vegetarian 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[3]/table"));
			// Check out the Customer Stats
			selenium.click("link=Customer Stats");
			assertEquals(
					"User Name Name # of Reservations ReserveUser Very Cute Girl 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[4]/table"));
			// Check out the Flight Stats
			selenium.click("link=Flight Stats");
			assertEquals(
					"Flight ID # of Reservations 1002 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[5]/table"));
		}

		// Created by Steven Chow
		// Scenario 4 Graphs are displayed
		// Test Case: 1 Graphs are displayed lol
		@Test
		public void test4() throws Exception {
			// Go to website
			selenium.open("");
			// Login as an Admin
			selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=id", "1");
			selenium.type("name=password", "pass0");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Manager Portal
			assertEquals("Manager Portal", selenium.getTitle());
			// Check if the "Flight Statistics" Button is present
			assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Stats Page
			assertEquals("Statistics", selenium.getTitle());
			// Check out the Seat Class Stats
			selenium.click("link=Seat Class Stats");
			assertEquals(
					"Seat Class Count across all flights Business 0 Economy 1 Economy Plus 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[2]/table"));
			// Check out the Meal Stats
			selenium.click("link=Meal Stats");
			assertEquals(
					"Meal Count across all flights Mcdonalds 1 Beef 0 Chicken 0 Vegetarian 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[3]/table"));
			// Check out the Customer Stats
			selenium.click("link=Customer Stats");
			assertEquals(
					"User Name Name # of Reservations ReserveUser Very Cute Girl 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[4]/table"));
			// Check out the Flight Stats
			selenium.click("link=Flight Stats");
			assertEquals(
					"Flight ID # of Reservations 1002 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[5]/table"));
		}

		// Created by Steven Chow
		// Scenario 5 Check if Flight Updates
		// Test Case: All 4 of them
		@Test
		public void test5() throws Exception {
			// Go to website
			selenium.open("/Website/");
			// Login as an Admin
			selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=id", "1");
			selenium.type("name=password", "pass0");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Manager Portal
			assertEquals("Manager Portal", selenium.getTitle());
			// Check if the "Flight Statistics" Button is present
			assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Stats Page
			assertEquals("Statistics", selenium.getTitle());
			// Check out the Seat Class Stats
			selenium.click("link=Seat Class Stats");
			assertEquals(
					"Seat Class Count across all flights Business 0 Economy 1 Economy Plus 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[2]/table"));
			// Check out the Meal Stats
			selenium.click("link=Meal Stats");
			assertEquals(
					"Meal Count across all flights Mcdonalds 1 Beef 0 Chicken 0 Vegetarian 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[3]/table"));
			// Check out the Customer Stats
			selenium.click("link=Customer Stats");
			assertEquals(
					"User Name Name # of Reservations ReserveUser Very Cute Girl 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[4]/table"));
			// Check out the Flight Stats
			selenium.click("link=Flight Stats");
			assertEquals(
					"Flight ID # of Reservations 1002 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[5]/table"));
			// Maka a Reservation
			selenium.click("//a[contains(@href, '/Website/index_manager.jsp?id=1')]");
			selenium.waitForPageToLoad("30000");
			selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
			selenium.waitForPageToLoad("30000");
			selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=username", "User1");
			selenium.type("name=password", "pass1");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			selenium.click("xpath=(//label[@onclick=''])[2]");
			selenium.click("xpath=(//input[@name='r1'])[2]");
			selenium.click("css=div > input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=firstName", "Beautiful");
			selenium.type("name=lastName", "Girl");
			selenium.type("name=seatID", "B10");
			selenium.click("//div[@id='page1']/div/label[2]/span");
			selenium.click("xpath=(//input[@name='mealOption'])[2]");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=credit", "200");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
			selenium.waitForPageToLoad("30000");
			selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Go login again
			selenium.type("name=id", "1");
			selenium.type("name=password", "pass0");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Check the Stats Again
			selenium.click("link=Seat Class Stats");
			assertEquals(
					"Seat Class Count across all flights Business 1 Economy 1 Economy Plus 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[2]/table"));
			// Check out the Meal Stats Again
			selenium.click("link=Meal Stats");
			assertEquals(
					"Meal Count across all flights Mcdonalds 1 Beef 1 Chicken 0 Vegetarian 0",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[3]/table"));
			// Check out the Customer Stats Again
			selenium.click("link=Customer Stats");
			assertEquals(
					"User Name Name # of Reservations ReserveUser Very Cute Girl 1 User1 Beautiful Girl 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[4]/table"));
			// Check out the Flight Stats Again
			selenium.click("link=Flight Stats");
			assertEquals(
					"Flight ID # of Reservations 1001 1 1002 1",
					selenium.getText("//html/body/div/div[2]/div/div/div/div[5]/table"));
		}

		// Created by Steven Chow
		// Scenario 6: Flight Stats not displayed
		// Test Case 1: No data present
		@Test
		public void test6() throws Exception {
			// Go to website
			selenium.open("");
			// Login as an Admin
			selenium.click("css=div.tile.bg-color-yellow > div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			selenium.type("name=id", "1");
			selenium.type("name=password", "pass0");
			selenium.click("css=input[type=\"submit\"]");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Manager Portal
			assertEquals("Manager Portal", selenium.getTitle());
			// Check if the "Flight Statistics" Button is present
			assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
			selenium.click("css=div.tile-content.icon");
			selenium.waitForPageToLoad("30000");
			// Check if we are on the Stats Page
			assertEquals("Statistics", selenium.getTitle());
			selenium.click("link=Seat Class Stats");
			// Check for a Message
			assertTrue(selenium
					.isTextPresent("No Data To Display. No Reservations are made."));
			selenium.click("link=Meal Stats");
			// Check for a Message
			assertTrue(selenium
					.isTextPresent("No Data To Display. No Reservations are made."));
			selenium.click("link=Customer Stats");
			// Check for a Message
			assertTrue(selenium
					.isTextPresent("No Data To Display. No Reservations are made."));
			selenium.click("link=Flight Stats");
			// Check for a Message
			assertTrue(selenium
					.isTextPresent("No Data To Display. No Reservations are made."));
		}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}

}
